package exam;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * 每个感染的格子一天后可以感染相邻的格子,求全部感染需要的天数
 *
 * @author shenenlu 2021年05月07日 下午16:53:20
 */
public class Infect {


    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while ((str = bufferedReader.readLine()) != null){
            System.out.println(processInfect(str));
        }
    }



    private static int processInfect(String str){
        if( str.contains("0") && !str.contains("1")){
            return -1;
        }
        if( str.contains("1") && !str.contains("0")){
            return -1;
        }
        String[] inputSplit = str.split(",");
        int n = ((Double) (Math.sqrt(inputSplit.length))).intValue();
        if( n * n != inputSplit.length){
            return  -1;
        }
        if( n < 1){
            return -1;
        }
        if( n == 2 ) {
            return 1;
        }
        int[][] origin = new int[n][n];
        for( int i = 0; i < n; i++){
            for( int j = 0; j < n; j++){
                origin[i][j] = Integer.parseInt( inputSplit[ i*n + j ]);
            }
        }

        InfectProcessor infectProcessor = new InfectProcessor(origin, n);
        infectProcessor.infect();
        return infectProcessor.days;
    }


    static class InfectProcessor {
        private int[][] infectMatrix;
        private int n;
        private int days;

        public InfectProcessor(int[][] infectMatrix, int n) {
            this.infectMatrix = infectMatrix;
            this.n = n;
            this.days = 0;
        }

        private static class Point{
            private int x;
            private int y;

            public Point() {
            }

            public Point(int x, int y) {
                this.x = x;
                this.y = y;
            }

            @Override
            public int hashCode() {
                return Objects.hash(x,y);
            }

            @Override
            public boolean equals(Object obj) {
                if( obj instanceof Point){
                    Point other = (Point) obj;
                    return other.x == x && other.y == y;
                }
                return false;
            }
        }

        private Set<Point> check(){
            printMatrix();
            Set<Point> infectedPointSet = new HashSet<>();
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (infectMatrix[i][j] == 1) {
                        infectedPointSet.add(new Point(i,j));
                    }
                }
            }
            return infectedPointSet;
        }

        private void printMatrix() {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    System.out.print(infectMatrix[i][j]);
                }
                System.out.println();
            }
            System.out.println();
        }

        private void infect() {
            Set<Point> todayInfectPointSet = new HashSet<>();
            Set<Point> infectedPointSet;
            while ( (infectedPointSet = check()).size() != n*n  ) {
                for( Point point : infectedPointSet){
                    int factXMin = Math.max(point.x - 1, 0);
                    int factXMax = Math.min(point.x + 1, n - 1);
                    int factYMin = Math.max(point.y - 1, 0);
                    int factYMax = Math.min(point.y + 1, n - 1);
                    todayInfectPointSet.add(new Point(factXMin, factYMin));
                    todayInfectPointSet.add(new Point(factXMin, point.y));
                    todayInfectPointSet.add(new Point(factXMin,factYMax));
                    todayInfectPointSet.add(new Point(point.x, factYMin));
                    todayInfectPointSet.add(new Point(point.x, factYMax));
                    todayInfectPointSet.add(new Point(factXMax, factYMin));
                    todayInfectPointSet.add(new Point(factXMax, point.y));
                    todayInfectPointSet.add(new Point(factXMax, factYMax));
                }
                for( Point point : todayInfectPointSet){
                    if( infectMatrix[point.x][point.y] == 0 ){
                        infectMatrix[point.x][point.y] = 1;
                    }
                }
                todayInfectPointSet.clear();
                days++;
            }
        }
    }


}
